<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/component/commonTagLib.jsp"%>
<!DOCTYPE html>
<html>
<head>
<title><spring:message code="message.lable.YiGouXuanMuLu"/></title>
<%@ include file="/WEB-INF/component/commonCSS.jsp"%>
<!--[if lt IE 9]>
        <script src="lib/js/html5shiv.min.js"></script>
        <script src="lib/js/respond.min.js"></script>
    <![endif]-->
</head>
<body class="skin-blue sidebar-mini fixed skin-blue-light-frame">
	<section class="content-header">
		<h1>采购目录维护</h1>
		<ol class="breadcrumb">
			<li><a href="${ctx}/home.jsp"><i class="fa fa-home"></i><spring:message code="message.lable.first"/></a></li>
			<li><a href="#">交易</a></li>
			<li class="active"><a href="#">采购目录维护</a></li>
		</ol>
	</section>

	<section class="content">
		<div class=" box box-success">
			<div class="box-body">
				<div class="input">
                    <div class="textalign1"><spring:message code="message.listForm.tongYongMing"/><spring:message code="message.yanZheng.maoHao"/></div>
                    <div class="boxinput">
                        <input type="text" class="textinput" id="drugName" name="drugName" placeholder="<spring:message code="message.listForm.tongYongMing"/>/<spring:message code="message.listForm.pinYinMa"/>" />
                    </div>
                    <%--<div class="textalign1"><spring:message code="message.listForm.yaoPinBianMa"/><spring:message code="message.yanZheng.maoHao"/></div>
                    <div class="boxinput">
                        <input type="text" class="textinput" id="drugCode" name="drugCode"  placeholder="<spring:message code="message.listForm.yaoPinBianMa"/>" />
                    </div>--%>
                    <div class="textalign"><spring:message code="message.listForm.shengChanQiYe"/><spring:message code="message.yanZheng.maoHao"/></div>
                    <div class="boxinput">
                        <input type="text" class="textinput" id="compName" name="compName" placeholder="<spring:message code="message.listForm.shengChanQiYe"/>/<spring:message code="message.listForm.pinYinMa"/>" />
                    </div>
					<div class="textalign"><spring:message code="message.listForm.PeiSongQiYe"/><spring:message code="message.yanZheng.maoHao"/></div>
                    <div class="boxinput">
                        <input type="text" class="textinput" id="delComp" name="delComp" placeholder="<spring:message code="message.listForm.PeiSongQiYe"/>/<spring:message code="message.listForm.pinYinMa"/>" />
                    </div>
                 </div>
                 <div class="input">
					
                   <div class="textalign1">药品来源<spring:message code="message.yanZheng.maoHao"/></div>
                        <div class="boxinput">
                        <select class="textinput"  id="goodsSource" name="goodsSource">
                        <option value=""><spring:message code="message.select.option"/></option>
                        <c:forEach items="${list }" var="list">
                            <option value="${list.dicCode }">${list.dicName }</option>
                        </c:forEach>
                        </select>
                    </div>
					<div class="textalign"><spring:message code="message.lable.status"/>：</div>
                    <div class="boxinput">
                        <select class="textinput"  id="status" name="status"> 
                            <option value=""><spring:message code="message.select.option"/></option>
                            <option value="1"><spring:message code="message.button.start"/></option>
                            <option value="0"><spring:message code="message.button.stop"/></option>
                        </select>
                    </div>
                    <div class="textalign" >
                       <button type="button" onclick="gridSerch();" class="btn btn-success btn-sm" id="selectDicCompType"><spring:message code="message.button.seachSpacing"/></button>
                    </div>
				</div>
			</div>
		</div>


		<table class="jqgrid" id="gridlist"></table>
		<div id="gridpage"></div>
		<div class=" box box-warning bottongroup">
			<label id="lxtj" onclick="window.location.href='${ctx}/purHospCatalog/toGoodsSelect.html';" class="btn btn-primary btn-sm">新 增</label>
			<%-- <label id="batchModifyData" onclick="batchModifyData()" class="btn btn-primary btn-sm"><spring:message code="message.button.saveSpacing"/></label> --%>
			<label id="batchChangeStatus" onclick="batchChangeStatus(1)" class="btn btn-primary btn-sm"><spring:message code="message.button.startSpacing"/></label>
			<label id="batchChangeStatus" onclick="batchChangeStatus(0)" class="btn btn-primary btn-sm"><spring:message code="message.button.stopSpacing"/></label>
		</div>
	</section>
	<%@ include file="/WEB-INF/component/commonJS.jsp"%>
	<script type="text/javascript">
	//绑定回车
    $('#projName,#drugCode,#drugName,#compName,#delComp,#status,#goodsSource').bind('keydown', function(event) {
        if (event.keyCode == "13") {
            $('#selectDicCompType').click();
        }
    });	
    $("#drugName").focus();//加载页面光标在第一个查询框内
        $(function () {
        	$("#projId").HNSelect({
                url: "${pageContext.request.contextPath}/selectController/getMyProj.html", data: { ID: '' },  defaultText: "<option value=''><spring:message code="message.select.option"/></option>",
                
            });            
            
        	$("#gridlist").jqGrid({
                url: "${ctx}/purHospCatalog/getHaveSelectData.html",
                mtype: "POST",
                datatype: "json",
                autowidth: true,
                shrinkToFit: false,
              //  height: 230,
                colNames: [
                           'hospProCatId',
                           'prodCompcode',
                           'hospCode',
                           'delCompId',
                           'projID',
                           '<spring:message code="message.jqGrid.cz"/>',
                           '中标价',
                           '中标后零售价',
                           '<spring:message code="message.listForm.CaiGouJia"/>',
                           '<spring:message code="message.listForm.yaoPinBianMa"/>',
                           '<spring:message code="message.listForm.tongYongMing"/>', 
                           '<spring:message code="message.listForm.jiXing"/>',
                           '<spring:message code="message.listForm.guiGe"/>',
                           '<spring:message code="message.listForm.zhuanHuanXiShu"/>',
                           '<spring:message code="message.listForm.baoZhuangDanWei"/>', 
                           '<spring:message code="message.listForm.caiZhi"/>',
                           '<spring:message code="message.listForm.shengChanQiYe"/>',
                           '国药准字',
                           '药品来源',
						   '药品类别',
                           '<spring:message code="message.listForm.shangBianMa"/>',
                           '项目名称',
                           '<spring:message code="message.listForm.PeiSongQiYe"/>',
                           '<spring:message code="message.lable.status"/>',
                           ''
                           
                           ],
                colModel: [
					{ name:'hospProCatId', width:60, hidden:true,align:"center", sortable:false  },
					{ name:'prodCompcode', width:60, hidden:true,align:"center", sortable:false  }, 
					{ name: 'hospCode', width:60, hidden:true,align:"center", sortable:false },//勾选目录ID
					{ name: 'delCompCode', width:60, hidden:true,align:"center", sortable:false  },//配送企业ID   
					{ name: 'projId', width:60, hidden:true,align:"center", sortable:false  },//项目ID   
					{ name: 'oper', index: 'ship',hidden:true,width:60, align: "center" , sortable:false,
						formatter:function(val, options, row) {
							if (row.setDel){
								return "<a href=\"javascript:$.HN.dialog.opendialog('delId"+row.hospProCatId+"','<spring:message code="message.button.SetPeiSongQiYe"/>','${ctx}/purGoodsSelect/examplest1.html?projId="+row.projId+"&goodsCode="+row.goodsCode+"&delId= "+row.hospProCatId+"&delTxt="+row.hospProCatId+"','800px','400px',operResult)\" style='background:url(${ctx}/lib/img/icon/icon.png) no-repeat -120px -100px' class='opIcon' title='<spring:message code="message.button.SetPeiSongQiYe"/>'></a>";
							} else {
								if ("${setBakDel}" == "true"){
									return "<a href=\"javascript:$.HN.dialog.opendialog('delId"+row.hospProCatId+"','<spring:message code="message.button.SetPeiSongQiYe"/>','${ctx}/purGoodsSelect/examplest1.html?projId="+row.projId+"&goodsCode="+row.goodsCode+"&delId= "+row.hospProCatId+"&delTxt="+row.hospProCatId+"','800px','400px',operResult)\" style='background:url(${ctx}/lib/img/icon/icon.png) no-repeat -120px -100px' class='opIcon' title='<spring:message code="message.button.SetPeiSongQiYe"/>'></a>"; 
								}
								return "";
							}
                    	} },
					
                    { name: 'proPriceLimit', index: 'ship', width: 80, align: "right", sortable:false,
                    formatter:function(val, options, row){
                    	return formatAmount(val, options, row);
                    }		
                    },
                    { name: 'retailPriceLimit', index: 'ship', width: 80, align: "right", sortable:false},
                    { name: 'proPrice', index: 'ship', hidden:true,width: 80, align: "right", sortable:false,
                            formatter:function(val, options, row) {
                                if ('${isSetCatalogPrice}' == 'true'){//配置项看是否可以修改采购价
                                    if (!row.setDel){//备案商品不可修改
                                        return val;
                                    } else {
                                        if (null != val && val > 0){
                                            return "<input class=\"\" style=\"width:75px;text-align: right;\" type=\"text\" id=\"proPrice"+row.hospProCatId+"\" value=\""+ val + "\" />";
                                        } else {
                                            return "<input class=\"\" style=\"width:75px;text-align: right;\" type=\"text\" id=\"proPrice"+row.hospProCatId+"\" value=\""+ row.proPriceLimit + "\" />";
                                        }
                                        
                                    }
                                } else {
                                    return val;
                                }
                            }
                    },
					{ name: 'drugCode',index:'drugCode',hidden:true,width:100,align:"center", sortable:false,
						formatter:function(cellvalue, options, row) {
							return "<a href=\"javascript:void(0);\" onclick=\"showDrugInfo('"+cellvalue+"', '"+row.qualityspecName+"')\">"+cellvalue+"</a>"
                    	}   },//药品编码
                    { name: 'qualityspecName', index: 'name', width: 160, align:"left" , sortable:false },
                    { name: 'formName', index: 'stock', width: 85 , align: "center", sortable:false },
                    { name: 'specName', index: 'ship', width: 85, align: "left" , sortable:false },
                    { name: 'factor', index: 'ship', width: 85, align: "center" , sortable:false },
                    { name: 'unitName', index: 'ship', width: 65, align: "center" , sortable:false },
                    { name: 'materialName', index: 'ship', hidden:true,width: 80, align: "left" , sortable:false },
                    { name: 'prodCompname', index: 'ship', width: 120, align: "center" , sortable:false/*,
						formatter:function(cellvalue, options, row) {
							return "<a href=\"javascript:void(0);\" onclick=\"showCompInfo('"+row.prodCompcode+"', '"+cellvalue+"')\">"+cellvalue+"</a>"
                    	}*/},
                    { name: 'drugMedicine', index: 'drugMedicine',width: 100, align: "center", sortable:false},
                    { name: 'goodsSource', index: 'goodsSource',width: 80, align: "center", sortable:false},
					{name:'drugAttr',index:'drugAttr',sortable: false, width:80, align:"left",sorttype:false,
						formatter : function(val, opts, rowdata) {
							var str = "";
							if (val == 1) {
								str = "国家基本药物";
							} else if (val == 2) {
								str = "非基本药物";
							} else if (val == 3) {
								str = "省增补基本药物";
							} 
							return str;
						}
					},
      		     	{ name: 'goodsCode',index:'goodsCode', width:100,align:"center", sortable:false,hidden:true  },//商品编码
                    { name: 'projName', index: 'sdate', width: 260, align: "center", sortable:false,hidden:true },
                    { name: 'delCompName', width: 190, align: "left", sortable:false},
                    { name: 'status',inde:'status', width : 50, align : 'center', sortable:false,
                    	cellattr:function (rowId, val, rawObject, cm) {
                            if(val == '停用'){
                                return 'style="color:red"';
                            }
                        },
                    	formatter : function(val, opts, rowdata) {
                    		var strSta = "";
                    		if(rowdata.status == 1){
                    			strSta += "<spring:message code="message.button.start"/>";
                    		}else{
                    			strSta += "<spring:message code="message.button.stop"/>";
                    		}
                    		return strSta;
                    	} },
                    { name: 'setDel', index: 'setDel', width: 80,hidden:true, align: "center", sortable:false}
                    	
                    
                ],
                rowNum: 20,
                rowList: [10, 20, 50,100],
                rownumbers: true,
                pager: "#gridpage",
                multiselect: true, 
                viewrecords: true,
                gridComplete: function(){
                	//序号列宽度自适应（参数为jqgridID）
                    autoRNWidth("gridlist");
                	//数据只有一条时默认选中（参数为jqgridID）
                    selectByOneData("gridlist");
            		var ids = jQuery("#gridlist").jqGrid('getDataIDs');
            	},
                caption: "药品列表",
                jsonReader: {
                    repeatitems: false,
                    id: "hospProCatId"
                }
            });
            $("#gridlist").jqGrid('navGrid', '#gridpage', { add: false, edit: false, del: false, search: false, refresh: false });
            $(window).trigger("resize");
            $("#gridlist").jqGrid("setGridHeight", create());
        });
        
        
        
        function gridSerch(page) {
        	var delComp = $.trim($("#delComp").val());
        	var projId = $.trim($("#projId").val());
        	var projName = $.trim($("#projName").val());
        	var drugCode = $.trim($("#drugCode").val());
        	var drugName = $.trim($("#drugName").val());
        	var compName = $.trim($("#compName").val());
        	var goodsSource = $.trim($("#goodsSource").val());
        	var status = $.trim($("#status").val());
    		$("#gridlist").jqGrid('setGridParam',{  
    	        datatype:'json',
    	        postData:{"delComp":delComp,
    	        	"projId":projId,
    	        	"projName":projName,
    	        		"drugCode":drugCode,
    	        		"qualityspecName":drugName,
    	        		"prodCompname":compName,
    	        		"goodsSource":goodsSource,
    	        		"status":status},
    	        		page:(page||1)
    	    }).trigger("reloadGrid"); //重新载入  
    	}
        
        
					//修改
					function batchModifyData() {
						var rowIds = $("#gridlist").jqGrid("getGridParam", "selarrrow") || [];
						if(rowIds.length <=0){
							top.$.HN.message.alert("请选择要操作的药品！", "<spring:message code="message.HN.alert.type"/>", "warn");
							return;
						}
						$.HN.message.confirm("确定保存吗？", '', '').on(function (e) {
				            if (e) {
						var list = [];
			    		for (var i=0; i<rowIds.length; i++) {
			    			rowId = rowIds[i];
			    			var rowdata = $("#gridlist").jqGrid('getRowData', rowId);
			    			var proprice = "";
			    			if (rowdata.setDel == "false"){
			    				proprice = rowdata.proPrice;
			    			} else {
			    				if("${isSetCatalogPrice}" == "true"){
			    					proprice = $.trim($("#proPrice"+rowdata.hospProCatId).val());
				    			} else {
				    				proprice = rowdata.proPrice;
				    			}
			    				if ($.trim(proprice).length < 1){
			    					top.$.HN.message.alert("<spring:message code="message.HN.alert.pleaseTianXieCaiGouJia"/><spring:message code="message.yanZheng.ganTanHao"/>", "<spring:message code="message.HN.alert.type"/>", "warn");
		                            return;
			    				}
			    			}
			    			list.push({ "delCompCode":rowdata.delCompCode,"hospProCatId":rowdata.hospProCatId,"goodsCode":rowdata.goodsCode,"proPrice":proprice,"proLimitPrice":rowdata.proPriceLimit,"drugName":rowdata.qualityspecName});
			    		}
			    		$.post("${ctx}/purHospCatalog/batchModifyData.html", {
			    			"array" : json.jsonToString(list)
			    		}, function(result) {
			    			if (result.success) {
			    				var curpage = $("#gridlist").jqGrid('getGridParam', 'page');
			    				gridSerch();
			    				top.$.HN.message.alert("操作成功！", "<spring:message code="message.HN.alert.type"/>", "success");
			    			} else {
			    				top.$.HN.message.alert(result.msg, "<spring:message code="message.HN.alert.type"/>", "error");
			    			}
			    		}, "json").error(function(err) {
			    			top.window.document.location = "${ctx}/login.html"; 
			    		});
					}
						 });
					} 
				            
			
		//批量
			function batchChangeStatus(status) {
				var msg = "<spring:message code="message.button.stop"/>";
				if(status == 1){
					var msg = "<spring:message code="message.button.start"/>";
				}
				//获取选中的ID及对应的名字（参数:jqgridId,名字列名，状态列名，当前操作即列表中显示的值），返回infos[0]是ID串，infos[1]是名字串
	        	var infos = batchSwitchStatus("gridlist","qualityspecName","status",msg,"药品");
	        	if ( !(infos instanceof Array)){
	        		return;
	        	}
			$.HN.message.confirm("确定"+msg+"以下药品吗？<br/>"+infos[1].replace(/\,/g,"<br/>"), '', '').on(function (e) {
	            if (e) {
	            	    var tempList= new Array();   
	            	    tempList = infos[0].split(",");
	            	    var list = [];
	            	    for (var i=0;i<tempList.length ;i++ ){   
	            	    	list.push({"hospProCatId":tempList[i],"status":status})
	            	    }   
			    		$.post("${ctx}/purHospCatalog/batchSwitchStatus.html", {
			    			"array" : json.jsonToString(list)
			    		}, function(result) {
			    			if (result.success) {
			    				gridSerch($("#gridlist").getGridParam("page"));
			    				top.$.HN.message.alert("成功"+msg+infos[0].split(",").length+"个药品", "<spring:message code="message.HN.alert.type"/>", "success");
			    			} else {
			    				top.$.HN.message.alert("操作失败！", "<spring:message code="message.HN.alert.type"/>", "error");
			    			}
			    		}, "json").error(function(err) {
			    			top.window.document.location = "${ctx}/login.html"; 
			    		});
	            }
			}); 
    	}
		 
		
			//回调
			  function operResult(val){
	        	var strs = new Array();
	            strs = val.split("|$");
	            	if (val.length>0) {
	            		$("#gridlist").jqGrid('setCell',strs[1],'delCompCode',strs[2]);
	            		$("#gridlist").jqGrid('setCell',strs[1],'delCompName',strs[3]);
					}	
					
	        }
		
		
		 
		 
    </script>
</body>
</html>